/*
 * Mario.cpp
 *
 *  Created on: 08/01/2012
 *      Author: Graciela
 */

#define FLOOR_HEIGHT 100

#include "Mario.h"
#include <math.h>
#include "../graphics/Common.h"

Mario::Mario() {
	left = right = false;
	vecX = 0;
	vecY = 0;
	friction = .9;
}

Mario::~Mario() {
}

void Mario::update() {
	int accX = 5;
	int accY = -5;

	vecX *= friction;
	if (abs(vecX) < .1)
		vecX = 0;

	vecY += accY;

	if (left)
		vecX -= accX;
	if (right)
		vecX += accX;

	if (getY() + vecY < FLOOR_HEIGHT)
		vecY = 0;

	getSpriteInstance()->getSprite()->nextFrame();
	translate(vecX, vecY);
}

void Mario::onKeyDown(SDL_Event * event) {
	switch (event->key.keysym.sym) {
	case SDLK_w:
		if (getY() <= FLOOR_HEIGHT)
			vecY += 50;
		break;
	case SDLK_s:
		break;
	case SDLK_a:
		left = true;
		break;
	case SDLK_d:
		right = true;
		break;
	default:
		break;
	}
}

void Mario::onKeyUp(SDL_Event * event) {
	switch (event->key.keysym.sym) {
	case SDLK_a:
		left = false;
		break;
	case SDLK_d:
		right = false;
		break;
	default:
		break;
	}
}
